package com.hclc.entity.model.series;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.logging.log4j.util.Strings;

import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: hef
 * 反序列化LocalTime
 * @Date: Create in 11:17 2020-02-29
 */
public class DeCustomLocalTimeSerializer extends StdDeserializer<LocalTime> {

    protected DeCustomLocalTimeSerializer() {
        super(LocalTime.class);
    }

    protected DeCustomLocalTimeSerializer(Class<?> vc) {
        super(vc);
    }

    protected DeCustomLocalTimeSerializer(JavaType valueType) {
        super(valueType);
    }

    protected DeCustomLocalTimeSerializer(StdDeserializer<?> src) {
        super(src);
    }

    @Override
    public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm:ss");
        String string = jsonParser.getText().trim();
        if (Strings.isBlank(string)) {
            return null;
        }
        LocalTime date = LocalTime.parse(string, df);
        return date;
    }
}
